个人专版PHP MVC框架(TZN Framework)

一直都在用别人的框架,虽说知道原理,但是没有实际去尝试写一个看看,下午没事看了一下CI的源码,确实短小精悍,萌生了自己尝试一下的念头,花了几个小时谢了一个超级简单的MVC框架,实现了基本的MVC,没有路由分发,没有安全验证,没有考虑扩展性,只支持Mysql等等等等,后期哥好好规划给整牛逼了,目前是0.0.1,嘿~

核心一共4个文件

Bootstrap.php

Controller.php

Model.php

View.php

先发上来让大家瞅瞅~

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

$app = "app";
$system = "system";

if(is_dir($app)){
$app = realpath($app)."\\";
}else{
exit("app path not found !");
}
if(is_dir($system)){
$system = realpath($system)."\\";
}else{
exit("system path not found !");
}
$app = str_replace("\\","/",$app);
$system = str_replace("\\","/",$system);
define("APP",$app);
define("SYSTEM",$system );

require SYSTEM."Bootstrap.php";

Bootstrap.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

if ( ! defined('SYSTEM')) exit('Go away!');
define("CORE","TZNPHP"); define("VERSION","0.0.1");
require(SYSTEM."Controller.php");
require(SYSTEM."View.php");
require(SYSTEM."Model.php");
$class = isset($_GET['c'])?$_GET['c']:"Index";
$method = isset($_GET['m'])?$_GET['m']:"index";
if(file_exists(APP."config.php")){
require(APP."config.php");
}else{
exit("Config file not found !");
}
if(file_exists(APP."controllers/".$class.".php")){
include(APP."controllers/".$class.".php");
if(class_exists($class)){
$VEW = new V();
$C = new $class();
if(method_exists($C,$method)){
if($class!="Index" OR $method!="index"){
$C--->$method();
}
$VEW->output();
}else{
exit("Method not found !");
}
}else{
exit("Class not found !");
}
}else{
exit("Class file not found !");
}

if(isset($C->conn)){
$C->conn->close();
}

function mysql_driver(){
global $config,$db;
if(isset($db)){return $db;}
$db = new mysqli($config['db']['hostname'],$config['db']['username'],$config['db']['password'],$config['db']['database']);
$db->set_charset($config['db']['char_set']);
return $db;
}

数据库的连接暂时写到这里了,以后但拉出来数据库的驱动目录

Controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
if ( ! defined('SYSTEM')) exit('Go away!'); 
class C{
public $conn;
public $Models;
public function view($filename,$data_array){
global $VEW;
$file_path = APP."/views/".$filename.".html";
if(file_exists($file_path)){
ob_start();
if(is_array($data_array)){
foreach($data_array as $key=-->$value){
$$key = $value;
}
}
include($file_path);
$VEW->contents = ob_get_contents();
@ob_end_clean();
}else{
exit("View file not found ! ");
}
}

public function model($filename){
if(isset($this->Models[$filename])){return $this->Models[$filename]; }
$file_path = APP."/models/".$filename.".php";
if(file_exists($file_path)){
$class_name = $filename."_model";
include($file_path);
if(class_exists($class_name)){
$this->conn = &mysql_driver();
$this->Models[$filename] = new $class_name($this->conn);
return $this->Models[$filename];
}else{
exit("Model class not found ! ");
}
}else{
exit("Model file not found ! ");
}
}
}

Model.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--?php  if ( ! defined('SYSTEM')) exit('Go away!'); class M {     public $conn;     public function __construct($conn){         $this--->conn = $conn;
}

public function query($sql){
$query = $this->conn->query($sql);
return $query;
}

public function fetch_one($sql){
$query = $this->conn->query($sql);
$result = $query->fetch_array();
return $result;
}

public function fetch_all($sql){
$query = $this->conn->query($sql);
while($row = $query->fetch_array()){
$result[] = $row;
}
return $result;
}

}

View.php

1
2
3
4
5
<!--?php  if ( ! defined('SYSTEM')) exit('Go away!'); class V{     public $contents;     public function output(){         echo $this--->contents;

}

}

View学CI,先是输出到缓冲区,再给打印出来,以后也加个Hook功能。一开始还准备像CI一样,实现get_instance,写完发现咱功能太少还用不到,随删除之~以后增加了用得到的地方在给上了~

GITHUB:https://github.com/toryzen/TZNPHP